package com.tg.appcommon.android;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.text.Spanned;

import androidx.annotation.DrawableRes;
import androidx.annotation.StringRes;

public class ResourcesUtil {
    /**
     * 1、在切换语言时，没有重启APP只是重新启动了MainActivity，所以Application不会更新
     * 直接通过Application获取的String就会出现翻译错误的问题
     * 2、在切换语言时，会同步更新ThunderApplication.getContext()中的Context
     * 所以，通过该context获取String是没有翻译问题的
     * <p>
     * 应用内通过资源id获取String时，都可以调用该全局方法
     */
    public static String getString(@StringRes int resId) {
        try {
            return getResources().getString(resId);
        } catch (Throwable ignored) {

        }
        return "";
    }

    public static String getString(@StringRes int resId, Object... formatArgs) {
        try {
            return getResources().getString(resId, formatArgs);
        } catch (Throwable ex) {

        }
        return "";
    }

    public static Resources getResources() {
        return TGApplicationBase.getApplicationContext().getResources();
    }

    public static Drawable drawableTransparent() {
        return new BitmapDrawable(null, (Bitmap) null);
    }

    @SuppressLint("UseCompatLoadingForDrawables")
    public static Drawable getDrawable(@DrawableRes int resid) {
        return getResources().getDrawable(resid);
    }
    public static class Compat {
        public static Spanned fromHtml(String source) {
            return Html.fromHtml(source);
        }
    }
}
